package org.apache.lucene.analysis.pinyin.utils;

/**
 * Created by liujt on 2017/4/12.
 */
public class StringUtils {

    public static boolean containsChinese(String s) {
        if(s != null && !"".equals(s.trim())) {
            for(int i = 0; i < s.length(); ++i) {
                if(isChinese(s.charAt(i))) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static boolean isChinese(char a) {
        return a >= 19968 && a <= 171941;
    }

    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();

        for(int i = 0; i < ch.length; ++i) {
            char c = ch[i];
            if(!isChinese(c)) {
                return false;
            }
        }

        return true;
    }

    public static int chineseCharCount(String s) {
        int count = 0;
        if(s != null && !"".equals(s.trim())) {
            for(int i = 0; i < s.length(); ++i) {
                if(isChinese(s.charAt(i))) {
                    ++count;
                }
            }

            return count;
        } else {
            return count;
        }
    }

    public static boolean isNumeric(String str) {
        boolean flag = true;
        if(str != null && str.length() != 0) {
            int i = str.length();

            while(true) {
                --i;
                if(i < 0) {
                    break;
                }

                if(!Character.isDigit(str.charAt(i))) {
                    flag = false;
                }
            }
        } else {
            flag = false;
        }

        return flag;
    }

}
